package com.peoit.honeycomb.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.peoit.honeycomb.application.SmsTemplateApplication;
import com.peoit.honeycomb.domain.SmsTemplate;
import com.peoit.honeycomb.dto.CommonResultJsonDto;

/**
 * 短信模板controller
 * 
 * @author zhenshis
 *
 */
@Controller
@RequestMapping("/admin/sms")
public class SmsTemplateController extends HoneycombBaseController {

	@Autowired
	SmsTemplateApplication sp;

	@RequestMapping("list")
	public ModelAndView list() {
		return view("/jsp/system/sms/list", this.sp.getAllSmsTemplate());
	}

	@RequestMapping("detail")
	public ModelAndView detail(String id) {
		return view("/jsp/system/sms/detail", this.sp.find(SmsTemplate.class, id));
	}

	@RequestMapping("toAdd")
	public ModelAndView toAdd() {
		return view("/jsp/system/sms/add");
	}

	@RequestMapping("toEdit")
	public ModelAndView toEdit(String id) {
		return view("/jsp/system/sms/add", sp.find(SmsTemplate.class, id));
	}

	@RequestMapping("save")
	public @ResponseBody CommonResultJsonDto save(SmsTemplate s) {
		CommonResultJsonDto result = CommonResultJsonDto.success("/admin/sms/list", "保存成功！");
		try {
			sp.saveSmsTemplate(s);
		} catch (Exception e) {
			result = CommonResultJsonDto.fail("系统错误！");
		}
		return result;
	}

	@RequestMapping("activate")
	public @ResponseBody CommonResultJsonDto activate(String type, String id) {
		CommonResultJsonDto result = CommonResultJsonDto.success("激活成功！");
		try {
			sp.activeSmsTemplate(type, id);
		} catch (Exception e) {
			result = CommonResultJsonDto.fail("系统错误！");
		}
		return result;
	}
}
